//测试
//用户注册，用户登录
//todo 手机号注册 手机号登陆 手机验证码获取
//获取(批量)用户信息
#include "etcd.hpp"
#include "channel.hpp"
#include "user.pb.h"
#include "utils.hpp"
#include <gtest/gtest.h>
DEFINE_bool(run_mode, false, "程序的运行模式, false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(user_service, "/service/user_service", "关注的服务名称");
ServiceChannel::ChannelPtr channel;


// TEST(用户注册, 用户名密码注册) {
//     chat_system::UserRegisterReq req;
//     req.set_request_id(uuid());
//     req.set_nickname("roboko2");
//     req.set_password("12345678");
//     chat_system::UserService_Stub stub(channel.get());
//     chat_system::UserRegisterRsp rsp;
//     brpc::Controller cntl;
//     stub.UserRegister(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
//     LOG_DEBUG("用户注册成功: {}", req.nickname());
// }


// TEST(用户登录, 用户名密码登录) {
//     chat_system::UserLoginReq req;
//     req.set_request_id(uuid());
//     req.set_nickname("roboko1");
//     req.set_password("12345678");
//     chat_system::UserService_Stub stub(channel.get());
//     chat_system::UserLoginRsp rsp;
//     brpc::Controller cntl;
//     stub.UserLogin(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
//     LOG_DEBUG("用户登录成功: {}", req.nickname());
// }


TEST(用户设置信息, 设置头像) {
    chat_system::SetUserAvatarReq req;
    req.set_request_id(uuid());
    req.set_user_id("6919-9f03ea75-0000");
    req.set_avatar("test_avatar_data");
    chat_system::UserService_Stub stub(channel.get());
    chat_system::SetUserAvatarRsp rsp;
    brpc::Controller cntl;
    stub.SetUserAvatar(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    LOG_DEBUG("用户设置头像成功: {}", req.user_id());
}

TEST(用户设置用户名, 设置为未存在的用户名) {
    chat_system::SetUserNicknameReq req;
    req.set_request_id(uuid());
    req.set_user_id("6919-9f03ea75-0000");
    req.set_nickname("roboko1");
    chat_system::UserService_Stub stub(channel.get());
    chat_system::SetUserNicknameRsp rsp;
    brpc::Controller cntl;
    stub.SetUserNickname(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success()); // 期望成功
    LOG_DEBUG("用户设置信息失败: {}", req.user_id());
}

TEST(用户设置用户名, 设置为已存在的用户名) {
    chat_system::SetUserNicknameReq req;
    req.set_request_id(uuid());
    req.set_user_id("6919-9f03ea75-0000");
    req.set_nickname("roboko2");
    chat_system::UserService_Stub stub(channel.get());
    chat_system::SetUserNicknameRsp rsp;
    brpc::Controller cntl;
    stub.SetUserNickname(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_FALSE(rsp.success()); // 期望失败，因为用户已经存在
    LOG_DEBUG("用户设置信息失败: {}", req.user_id());

}

TEST(用户设置签名, 设置1) {
    chat_system::SetUserDescriptionReq req;
    req.set_request_id(uuid());
    req.set_user_id("6919-9f03ea75-0000");
    req.set_description("ATRI-MY-DEAR-MOMENTS");
    chat_system::UserService_Stub stub(channel.get());
    chat_system::SetUserDescriptionRsp rsp;
    brpc::Controller cntl;
    stub.SetUserDescription(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    LOG_DEBUG("用户设置签名成功: {}", req.user_id());
}


TEST(用户设置电话, 设置为非法电话号码) {
    chat_system::SetUserPhoneNumberReq req;
    req.set_request_id(uuid());
    req.set_user_id("6919-9f03ea75-0000");
    req.set_phone_number("12345678901");
    chat_system::UserService_Stub stub(channel.get());
    chat_system::SetUserPhoneNumberRsp rsp;
    brpc::Controller cntl;
    stub.SetUserPhoneNumber(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_FALSE(rsp.success()); // 期望失败，因为电话号码格式不正确
}


// TEST(用户设置电话, 设置为合法电话号码) {
//     chat_system::SetUserPhoneNumberReq req;
//     req.set_request_id(uuid());
//     req.set_user_id("6919-9f03ea75-0000");
//     req.set_phone_number("13800138000");
//     chat_system::UserService_Stub stub(channel.get());
//     chat_system::SetUserPhoneNumberRsp rsp;
//     brpc::Controller cntl;
//     stub.SetUserPhoneNumber(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success()); // 期望成功
//     LOG_DEBUG("用户设置电话成功: {}", req.user_id());
// }


// TEST(获取用户信息, 获取单个用户信息) {
//     chat_system::GetUserInfoReq req;
//     req.set_request_id(uuid());
//     req.set_user_id("6919-9f03ea75-0000");
//     chat_system::UserService_Stub stub(channel.get());
//     chat_system::GetUserInfoRsp rsp;
//     brpc::Controller cntl;
//     stub.GetUserInfo(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
//     LOG_DEBUG("获取单个用户信息成功: {}", req.user_id());
//     LOG_DEBUG("用户信息: 用户ID: {}, 昵称: {}, 签名: {}, 电话: {}, 头像大小: {}",
//               rsp.user_info().user_id(),
//               rsp.user_info().nickname(),
//               rsp.user_info().description(),
//               rsp.user_info().phone(),
//               rsp.user_info().avatar().size());
// }

TEST(获取用户信息, 批量获取) {
    chat_system::GetMultiUserInfoReq req;
    req.set_request_id(uuid());
    req.mutable_users_id()->Add("6919-9f03ea75-0000");
    req.mutable_users_id()->Add("e7b5-538ed478-0001");
    chat_system::UserService_Stub stub(channel.get());
    chat_system::GetMultiUserInfoRsp rsp;
    brpc::Controller cntl;
    stub.GetMultiUserInfo(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    LOG_DEBUG("批量获取用户信息成功");
    for (const auto& user_info : rsp.users_info()) {
        LOG_DEBUG("用户ID: {}, 昵称: {}, 签名: {}, 电话: {}, 头像大小: {}",
                user_info.second.user_id(),
                user_info.second.nickname(),
                user_info.second.description(),
                user_info.second.phone(),
                user_info.second.avatar().size());
    }
}
int main() {
    init_logger(FLAGS_log_file, int_log_level_map(FLAGS_log_level), FLAGS_run_mode);
    testing::InitGoogleTest();
    ServiceManager::ptr service_manager = std::make_shared<ServiceManager>();
    service_manager->declared(FLAGS_user_service);
    auto put_cb = std::bind(&ServiceManager::onServiceOnline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&ServiceManager::onServiceOffline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    Discovery::ptr dclient = std::make_shared<Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    //获取用户服务信道管理对象
    auto channel_manager = service_manager->choose(FLAGS_user_service);
    if(channel_manager == nullptr) {
        LOG_DEBUG("获取文件服务信道失败");
        return -1;
    }
    // 获取用户服务信道
    channel = channel_manager->choose();
    if(channel == nullptr) {
        LOG_DEBUG("获取用户服务信道失败");
        return -1;
    }

    return RUN_ALL_TESTS();
}